//
//  DownloadManagement.m
//  51-Tour
//
//  Created by xiang ying on 14-4-3.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "DownloadManagement.h"
#import "ServiceManage.h"
#import "db_Downloading.h"

static DownloadManagement *_download = nil;

@interface DownloadManagement(){
    NSMutableDictionary     *targetsDic;
    NSOperationQueue        *_queue;
}

@end

@implementation DownloadManagement

+(instancetype)instance{
    if (!_download) {
        _download = [[DownloadManagement alloc] init];
    }
    return _download;
}

-(void)dealloc{

}

-(instancetype)init{
    self = [super init];
    if (self) {
        targetsDic = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)addTarget:(ScenicScene*)target{
    __block BOOL first = YES;

    NSString *filename = [NSString stringWithFormat:@"%@_%@.zip",target.scenicId,target.language];
    NSOperation *operation = [ServiceManage downloadZip:target.zipFile fileName:filename completion:^(RespondDTO *respond) {
        if (respond.flag == respond_success) {
            //下载完成
            target.isDownloading = NO;
            target.progress = target.zipSize;
            [db_Downloading updateDownloadInfo:target];
            NSLog(@"complete:%f",target.progress);
        }else if(respond.flag == respond_receicedata){
            //更新进度
            if (first) {
                target.progress = [respond.data floatValue];
                first = NO;
            }else{
                target.progress = target.progress+[respond.data floatValue];
            }
            [db_Downloading updateDownloadInfo:target];
            NSLog(@"progress:%f",target.progress);
        }else if(respond.flag == respond_nonet){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[DataCenter instance] getStringForKey:@"Tips"] message:@"没有网络" delegate:nil cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Sure"] otherButtonTitles: nil];
            [alert show];
        }else if (respond.flag == respond_timeout){
            [self pauseDownload:target];
        }
    }];
    if (operation) {
        target.isDownloading = YES;
        if (target.needUpdate == YES) {
            target.needUpdate = NO;
            target.progress = 0;
            //需要删除未下完的文件
            NSString *filename = [NSString stringWithFormat:@"%@_%@",target.scenicId,target.language];
            NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip",ZIPDOCUMENTPATH,filename];
            NSString *zipCathepath = [NSString stringWithFormat:@"%@/%@.zip",ZIPCACHEPATH,filename];
            [[NSFileManager defaultManager] removeItemAtPath:zipCathepath error:nil];
            [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];

            [db_Downloading updateDownloadInfo:target];
        }
        [targetsDic setObject:operation forKey:target.zipFile];
        [db_Downloading addDownloadInfo:target];
        [[NSNotificationCenter defaultCenter] postNotificationName:UPDATELIST object:nil];
    }
}

- (void)deleteTarget:(ScenicScene*)target{
    [self pauseDownload:target];
    [db_Downloading deleteHistory:target];
    [[NSNotificationCenter defaultCenter] postNotificationName:UPDATELIST object:nil];
    target.progress = 0;
    //同时删除本地文件
    NSString *filename = [NSString stringWithFormat:@"%@_%@",target.scenicId,target.language];
    NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip",ZIPDOCUMENTPATH,filename];
    NSString *zipCathepath = [NSString stringWithFormat:@"%@/%@.zip",ZIPCACHEPATH,filename];
    NSString *unzipPath = [NSString stringWithFormat:@"%@/%@",UNZIPDOCUMENTPATH,filename];
    [[NSFileManager defaultManager] removeItemAtPath:zipCathepath error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:unzipPath error:nil];
    
}

- (void)pauseDownload:(ScenicScene*)target{
    target.isDownloading = NO;
    NSOperation*operation = targetsDic[target.zipFile];
    [operation cancel];
    [targetsDic removeObjectForKey:target.zipFile];
    [db_Downloading updateDownloadInfo:target];
}

@end
